package com.sangeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sangeng.constants.SystemConstants;
import com.sangeng.domain.ResponseResult;
import com.sangeng.domain.entity.Link;
import com.sangeng.domain.entity.Role;
import com.sangeng.domain.vo.LinkVo;
import com.sangeng.domain.vo.PageVo;
import com.sangeng.mapper.LinkMapper;
import com.sangeng.service.LinkService;
import com.sangeng.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {

    // 查询所有审核通过的友链
    @Override
    public ResponseResult getAllLink() {

        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Link::getStatus, SystemConstants.LINK_STATUS_NORMAL);
        List<Link> linkList = list(queryWrapper);

        // 转换Vo
        List<LinkVo> LinkVo = BeanCopyUtils.copyBeanList(linkList, LinkVo.class);
        // 封装返回
        return ResponseResult.okResult(LinkVo);
    }

    /**
     * 分页查询所有友链
     * @param link
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageVo selectLinkPage(Link link, Integer pageNum, Integer pageSize) {

        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        // 根据友链名称进行模糊查询
        queryWrapper.like(StringUtils.hasText(link.getName()),Link::getName,link.getName());
        // 根据状态进行查询
        queryWrapper.eq(StringUtils.hasText(link.getStatus()),Link::getStatus,link.getStatus());

        // 分页构造器
        Page<Link> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page,queryWrapper);

        //转换成VO

        List<Link> linkList = page.getRecords();

        List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(linkList, LinkVo.class);

        PageVo pageVo = new PageVo();
        pageVo.setTotal(page.getTotal());
        pageVo.setRows(linkVos);
        return pageVo;
    }
}
